package com.xinchao.controller;

import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xinchao.constant.ResponseConstant;
import com.xinchao.export.util.SimpleExportUtil;
import com.xinchao.model.sys.JsonResponse;
import com.xinchao.model.vo.DataReportPointLocationVO;
import com.xinchao.model.vo.DataReportVO;
import com.xinchao.service.DataReportService;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;

/**
 * @author dxy
 * @date 2018/10/11 18:20
 */
@Controller
@RequestMapping(value = "/export")
public class ExportController {
	private static Logger logger = LoggerFactory.getLogger(ExportController.class);

	@Autowired
	private DataReportService dataReportService;
	/**
	 * 导出总体数据
	 */
	@RequestMapping(value = "/exportTotalData")
	public void exportTotalData(String advertisementName, String startTime, String endTime, HttpServletResponse response) {
		try (OutputStream out = response.getOutputStream()){
			//开始时间
			if (StringUtils.isBlank(startTime)) {
				generateRespnseContent(response, "开始时间不能为空");
			}
			//结束时间
			if (StringUtils.isBlank(endTime)) {
				generateRespnseContent(response, "结束时间不能为空");
			}
			List<DataReportVO> exportList = dataReportService.listTotalData(advertisementName, startTime, endTime);
			if (CollectionUtils.isEmpty(exportList)) {
				exportList = Lists.newArrayList();
				exportList.add(new DataReportVO());
			}
			//设置输出类型
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			//设置文件名
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("总体数据报表列表.xls").getBytes(), "ISO-8859-1"));
			SimpleExportUtil.exportToExcel(exportList, "导出总体数据细报表列表").write(out);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ExportController exportTotalData 导出总体数据报表失败：" + e.getMessage());
		}
	}

	/**
	 * 导出时段分析
	 */
	@RequestMapping(value = "/exportTimeFrameAnalysisData")
	public void exportTimeFrameAnalysisData(String advertisementName, String startTime, HttpServletResponse response) {
		try (OutputStream out = response.getOutputStream()){
			//开始时间
			if (StringUtils.isBlank(startTime)) {
				generateRespnseContent(response, "开始时间不能为空");
			}
			List<DataReportVO> exportList = dataReportService.listTimeFrameAnalysisData(advertisementName, startTime);
			if (CollectionUtils.isEmpty(exportList)) {
				exportList = Lists.newArrayList();
				exportList.add(new DataReportVO());
			}
			//设置输出类型
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			//设置文件名
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("时段分析报表列表.xls").getBytes(), "ISO-8859-1"));
			SimpleExportUtil.exportToExcel(exportList, "导出时段分析报表列表").write(out);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ExportController exportTimeFrameAnalysisData 导出时段分析失败：" + e.getMessage());
		}
	}

	/**
	 * 导出点位分析
	 */
	@RequestMapping(value = "/exportPointLocationData")
	public void exportPointLocationData(String advertisementName, String startTime, String endTime, String deviceNumber, HttpServletResponse response) {
		try (OutputStream out = response.getOutputStream()){
			//开始时间
			if (StringUtils.isBlank(startTime)) {
				generateRespnseContent(response, "开始时间不能为空");
			}
			//结束时间
			if (StringUtils.isBlank(endTime)) {
				generateRespnseContent(response, "结束时间不能为空");
			}
			//机器码
			if (StringUtils.isBlank(deviceNumber)) {
				generateRespnseContent(response, "机器码不能为空");
			}
			List<DataReportPointLocationVO> exportList = dataReportService.listDataReportPointLocationData(advertisementName, startTime, endTime, deviceNumber);
			if (CollectionUtils.isEmpty(exportList)) {
				exportList = Lists.newArrayList();
				exportList.add(new DataReportPointLocationVO());
			}
			//设置输出类型
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			//设置文件名
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("点位分析报表列表.xls").getBytes(), "ISO-8859-1"));
			SimpleExportUtil.exportToExcel(exportList, "导出点位分析报表列表").write(out);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ExportController exportPointLocationData 导出点位分析失败：" + e.getMessage());
		}
	}

	/**
	 * 导出所有点位分析
	 */
	@RequestMapping(value = "/exportAllPointLocationData")
	public void exportAllPointLocationData( HttpServletResponse response) {
		try (OutputStream out = response.getOutputStream()){
			List<DataReportPointLocationVO> exportList = dataReportService.listAllDataReportPointLocationData();
			if (CollectionUtils.isEmpty(exportList)) {
				exportList = Lists.newArrayList();
				exportList.add(new DataReportPointLocationVO());
			}
			//设置输出类型
			response.setContentType("application/vnd.ms-excel;charset=utf-8");
			//设置文件名
			response.setHeader("Content-Disposition", "attachment;filename=" + new String(("导出所有点位分析报表列表.xls").getBytes(), "ISO-8859-1"));
			SimpleExportUtil.exportToExcel(exportList, "导出所有点位分析报表列表").write(out);
		} catch (Exception e) {
			e.printStackTrace();
			logger.error("ExportController exportAllPointLocationData 导出所有点位分析失败：" + e.getMessage());
		}
	}

	/**
	 * 生产响应内容
	 * @param httpResponse HttpServletResponse
	 * @param content 提示内容
	 * @throws IOException
	 */
	private void generateRespnseContent(HttpServletResponse httpResponse, String content) throws IOException {
		//封装返回信息
		JSONObject jsonObject = new JSONObject();
		jsonObject.put(ResponseConstant.RESPONSE_JSON_DATA, "");
		jsonObject.put(ResponseConstant.RESPONSE_JSON_MSG, content);
		jsonObject.put(ResponseConstant.RESPONSE_JSON_CODE, ResponseConstant.CODE_FAIL);
		//写入ServletOutputStream
		ServletOutputStream outputStream = httpResponse.getOutputStream();
		outputStream.write(jsonObject.toJSONString().getBytes());
		outputStream.flush();
	}
}
